package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ListUtil;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerBannerAdapter;
import com.qire.common.widget.adapter.auto.SummerComplexBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.R;
import com.qire.manhua.controller.RecommendHotController;
import com.qire.manhua.dal.service.RecommendHotService;
import com.qire.manhua.databinding.LayoutHomeRecommendHotBannerItemBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendHotSlideZoneBinding;
import com.qire.manhua.model.entity.AppAdvert;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 首页-推荐页-热门（推荐）
 */
public class RecommendHotViewModel extends SmartRefreshViewModel<IViewModel> {

    private RecommendHotService hotService = new RecommendHotService(this);

    private final HotSlideZoneViewModel hotSlideZoneViewModel = new HotSlideZoneViewModel();

    private final ArrayList<AutoVariable> moduleList = new ArrayList();
    private final SummerComplexBindAdapter moduleAdapter = new SummerComplexBindAdapter(moduleList);


    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(RecommendHotController controller) {
        refreshUI();
        hotSlideZoneViewModel.setEventProcess(controller);
        moduleAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        hotSlideZoneViewModel.setEventProcess(null);
        moduleAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    // region    #业务

    public void loadHotModuleData() {
        hotService.loadPageData();
    }

    public void clearBannerList() {
        hotSlideZoneViewModel.clearBannerList();
    }

    public void addBannerItemViewModel(AppAdvert appAdvert) {
        hotSlideZoneViewModel.addBannerItemViewModel(appAdvert);
    }

    public void refreshBannerAdapter() {
        hotSlideZoneViewModel.refreshBannerAdapter();
    }

    public void clearModuleList() {
//        moduleAdapter.notifyItemRangeRemoved(1, moduleList.size() - 1);
        moduleList.clear();
        moduleList.add(hotSlideZoneViewModel);
    }
    public void addModule(AutoVariable[] modules) {
        if(ObjectUtil.nonNull(modules)) {
            for(AutoVariable module : modules) {
                if(ObjectUtil.nonNull(module)) {
                    moduleList.add(module);
                }
            }
        }
    }
    public void refreshModuleAdapter() {
        moduleAdapter.notifyItemChanged(0, "full");
    }

    // endregion #业务

    // region #UI操作

    public void refreshUI() {
        refreshEmptyDataUI();
    }

    public void refreshEmptyDataUI() {
        boolean isEmpty = ListUtil.isEmpty(moduleList);
        set("errorLayoutPanelVisible", isEmpty ? View.VISIBLE : View.GONE);
    }

    // endregion

    // region #UI属性绑定

    public MutableLiveData<Integer> getErrorLayoutPanelVisible() {
        return get("errorLayoutPanelVisible");
    }

    public RecyclerView.Adapter getModuleAdapter() {
        return moduleAdapter;
    }
    // endregion

    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_hot_slide_zone)
    public class HotSlideZoneViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendHotSlideZoneBinding> {

        private final ArrayList<HotBannerItemViewModel> bannerList = new ArrayList<>();
        private final SummerBannerAdapter bannerAdapter = new SummerBannerAdapter(bannerList, R.layout.layout_home_recommend_hot_banner_item);

        @Override
        public void bindVariable(LayoutHomeRecommendHotSlideZoneBinding viewBind) {
            viewBind.setHotSlideZoneViewModel(this);
        }

        public void setEventProcess(AutoVariable autoVariable) {
            bannerAdapter.setEventProcess(autoVariable);
        }

        public void clearBannerList() {
            bannerAdapter.notifyItemRangeRemoved(0, bannerList.size());
            bannerList.clear();
        }

        public void addBannerItemViewModel(AppAdvert appAdvert) {
            bannerList.add(new HotBannerItemViewModel(appAdvert));
        }

        public void refreshBannerAdapter() {
            bannerAdapter.notifyItemChanged(0, "full");
        }

        public SummerBannerAdapter getBannerAdapter() {
            return bannerAdapter;
        }

        public class HotBannerItemViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendHotBannerItemBinding> {

            private final AppAdvert appAdvert;

            public HotBannerItemViewModel(AppAdvert appAdvert) {
                this.appAdvert = appAdvert;
            }

            @Override
            public void bindVariable(LayoutHomeRecommendHotBannerItemBinding viewBind) {
                viewBind.setHotBannerItemViewModel(this);
            }

            public void doAction() {
                appAdvert.doAction();
            }

            @Bindable
            public String getAdvertImage() {
                return appAdvert.advertImage;
            }

        }

    }

}
